package com.eway.viewanimator;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class FrameAnimationActivity extends AppCompatActivity {

	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_frame_animation);

		imageView = findViewById(R.id.frame);

		final AnimationDrawable drawableAnimal = (AnimationDrawable) imageView.getBackground();


		findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!drawableAnimal.isRunning()) {
					drawableAnimal.start();
				}


			}
		});
		findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (drawableAnimal.isRunning()) {
					drawableAnimal.stop();
				}

			}
		});


		findViewById(R.id.build).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AnimationDrawable animationDrawable = new AnimationDrawable();
                // 要根据图片的起始数字开始
				for (int i = 1; i <= 14; i++) {
					int id = getResources().getIdentifier("list_icon_gif_playing" + i, "drawable", getPackageName());
					Drawable drawableId = getResources().getDrawable(id);
					animationDrawable.addFrame(drawableId, 60);
				}
				animationDrawable.setOneShot(false);
				imageView.setBackgroundDrawable(animationDrawable);
                animationDrawable.start();
			}
		});
	}
}
